from PyQt5.QtWidgets import *
from PyQt5 import QtGui
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import time
import pymysql
import sys
import pickle
import database.admin.setting as setting
import interface.admin.admin_main0_Window as admin_main0_Window
import interface.admin.schedule_course as scW
import interface.admin.sch_cou_Window as small_scW
class schedule_course_window(QMainWindow,scW.Ui_Form):
    def __init__(self):
        QMainWindow.__init__(self)
        scW.Ui_Form.__init__(self)
        self.setupUi(self)
        self.setWindowTitle("排课")
        self.read()
        self.table.setColumnCount(5)
        self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        self.table.setHorizontalHeaderLabels(['课程号', '课程名', '学分', '授课老师', '排课'])
        self.init_table()
        self.quit.clicked.connect(self.close)
        self.back.clicked.connect(self.load_pre)
        self.flush.clicked.connect(self.init_table)
    def load_pre(self):
        self.pre_window=admin_main0_Window.admin_main0_window()
        self.pre_window.show()
        self.close()
    def read(self):
        with open("admin/time.pkl",'rb') as f:
            _=pickle.load(f)
            end=pickle.load(f)
            term=pickle.load(f)
        self.term=term
        self.now_term.setText(self.term)
    def sch_but(self,cid):
        widget=QWidget()
        sel=QPushButton("排课")
        q=small_scW.sch_cou_window(cid)
        def load_():
            print(cid+"is clicked ")
            #QMessageBox.about(self, "提示", "是否选课")
            q.show()
        sel.clicked.connect(load_)
        return sel
    def init_table(self):
        self.connect = pymysql.connect(**setting.config)
        self.cursor = self.connect.cursor()
        sql_="select id,cname,credit,tname from course c1 where not exists (select * from course_schedule c2 where c2.cid \
                                    =c1.id and year_time=%s)"
        self.cursor.execute(sql_,self.term)
        re=self.cursor.fetchall()
        self.disp(re)
        self.cursor.close()
        self.connect.close()
        self.close()
        self.show()
    def disp(self,re):
        self.table.setRowCount(0)
        row = self.table.rowCount()
        # self.table.setRowCount(row+1)
        re_len = len(re)
        # print(re[2])
        for l in range(re_len):
            # print(l)
            row = self.table.rowCount()
            self.table.setRowCount(row + 1)
            check = QtWidgets.QTableWidgetItem()
            check.setCheckState(QtCore.Qt.Unchecked)
            # ['课程号', '课程名', '学分', '授课老师', '排课']
            self.table.setItem(row, 0, QTableWidgetItem(re[l][0]))
            self.table.setItem(row, 1, QTableWidgetItem(re[l][1]))
            self.table.setItem(row, 2, QTableWidgetItem(str(re[l][2])))
            self.table.setItem(row, 3, QTableWidgetItem(re[l][3]))
            sch = self.sch_but(re[l][0])
            # sel.setDown(True)
            # self.table.setItem(row,6,check)
            self.table.setCellWidget(row, 4, sch)

if __name__=='__main__':
    app=QApplication(sys.argv)
    md=schedule_course_window()
    md.show()
    sys.exit(app.exec_())